/*
 * ConnectionPalette.java
 *
 * Created on 3 wrzesień 2007, 16:11
 *
 */

package pl.krakow.ae.knp.wikiboard.gui.palette;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import pl.krakow.ae.knp.wikiboard.gui.bundles.Lang;

/**
 *
 * @author slawek
 */
public class ConnectionPalette extends JPanel {
    
    private JLabel lbBlank;
    private JButton btConnect;
    private JTextField txAddress;
    
    private PaletteEventListener listener;
    
    private OtherPaletteEventType mode = OtherPaletteEventType.CONNECT;
    
    /** Creates a new instance of ConnectionPalette */
    public ConnectionPalette(PaletteEventListener listener) {
        this.listener = listener;
        addComponents();
        layoutComponents();
    }
    
    private void addComponents() {
        lbBlank = new JLabel();
        lbBlank.setPreferredSize(new Dimension(100, 20));
        
        txAddress = new JTextField();
        txAddress.setPreferredSize(new Dimension(100, 20));
        txAddress.setText("localhost:44444");
        txAddress.addKeyListener(new java.awt.event.KeyAdapter() {
            
        });
        
        btConnect = new JButton();
        btConnect.setPreferredSize(new Dimension(100, 30));
        btConnect.setOpaque(true);
        btConnect.setText(Lang.get("connect"));
        btConnect.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                setBtConnectTextAndListener();
            }
        });
    }
    
    private void layoutComponents() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        add(lbBlank, c);
        add(txAddress, c);
        add(btConnect, c);
    }
    
    private void setBtConnectTextAndListener() {
        listener.paletteModified( new OtherPaletteEvent( this.mode ) );
        if( this.mode ==  OtherPaletteEventType.CONNECT && listener.isConnected()) {
            btConnect.setText(Lang.get("disconnect"));
            this.mode = OtherPaletteEventType.DISCONNECT;
            txAddress.setEditable(false);
        } else if ( this.mode == OtherPaletteEventType.DISCONNECT ) {
            btConnect.setText(Lang.get("connect"));
            this.mode = OtherPaletteEventType.CONNECT;
            txAddress.setEditable(true);
        }
    }
    
    String getTxAddress() {
        return txAddress.getText();
    }
}
